/**
 * 系统管理员
 */
package dms.model.role;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import dms.dto.DormSupervisorDTO;
import dms.dto.StudentAffairTeacherDTO;
import dms.model.Building;
import dms.model.School;
import dms.model.exception.NoSuchEntityException;
import dms.repository.BuildingRepository;
import dms.repository.SchoolRepository;
import dms.repository.SystemAdministratorRepository;

/**
 * @author 李健
 *
 */
public class SystemAdministrator {
	
	
	
	//实体属性
	private Account account;
	private Long id;
	
	public SystemAdministrator() {
		super();	
	}

	/**
	 * @return the account
	 */
	public Account getAccount() {
		return account;
	}

	/**
	 * @param account the account to set
	 */
	public void setAccount(Account account) {
		this.account = account;
	}

	/**
	 * @return the id
	 */
	public Long getId() {
		return id;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(Long id) {
		this.id = id;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		SystemAdministrator other = (SystemAdministrator) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}

	
	/*******************************************************************************************//*
	// Domain operation 
	
	public void addStudentAffairTeacher(StudentAffairTeacherDTO teacher) {
		StudentAffairTeacher studentAffairTeacher = new StudentAffairTeacher();
		Account account = new Account();
		account.setRole(Role.STUDENT_AFFAIR_TEACHER);
		
		School school = this.schoolRepository.findSchoolById(teacher.getSchool());
		try {
			BeanUtils.copyProperties(account, teacher);
		} catch (IllegalAccessException e) {
			log.error(e.getMessage());
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			log.error(e.getMessage());
			e.printStackTrace();
		}
		//确保account 和studentAffairTeacher 的 Id为空才能正确插入
		Date date = new Date();
		account.setRegisterDate(date);
		account.setModifyDate(date);
		account.setId(null);
		studentAffairTeacher.setId(null);
		studentAffairTeacher.setSchool(school);
		studentAffairTeacher.setAccount(account);
		
		this.systemAdministratorRepository.createStduentAffairTeacher(studentAffairTeacher);
	}
	
	public void updateStudentAffairTeacher(StudentAffairTeacherDTO teacher) throws NoSuchEntityException {
		StudentAffairTeacher studentAffairTeacher = this.systemAdministratorRepository.findStudentAffairTeacherById(teacher.getId());
		
		if (studentAffairTeacher == null) {
			throw new NoSuchEntityException("没有该实体");
		}
		Account account = new Account(Role.STUDENT_AFFAIR_TEACHER);
		
		School school = this.schoolRepository.findSchoolById(teacher.getSchool());
		
		if (school == null) {
			throw new NoSuchEntityException("没有该实体");
		}
		try {
			BeanUtils.copyProperties(account, teacher);
		} catch (IllegalAccessException e) {
			log.error(e.getMessage());
		} catch (InvocationTargetException e) {
			log.error(e.getMessage());
		}
		//确保account 和studentAffairTeacher 有一样的 Id 才能正确修改 一对一主键映射
		account.setId(teacher.getId());
		
		//记住要修改modifyDate
		account.setModifyDate(new Date());
		studentAffairTeacher.setId(teacher.getId());
		studentAffairTeacher.setSchool(school);
		studentAffairTeacher.setAccount(account);
		
		this.systemAdministratorRepository.updateStduentAffairTeacher(studentAffairTeacher);
	}
	
	public void deleteStudentAffairTeacherById(Long id) {
		this.systemAdministratorRepository.deleteStudentAffairTeacherById(id);
	}
	
	public void addDormSupervisor(DormSupervisorDTO dormSupervisorInfo) {
		DormSupervisor dormSupervisor = new DormSupervisor();
		Building building = this.buildingRepository.findById(dormSupervisorInfo.getBuilding());
		
		Account account = new Account(Role.DORM_SUPERVISOR);
		
		try {
			BeanUtils.copyProperties(account, dormSupervisorInfo);
		} catch (IllegalAccessException e) {
			log.error(e.getMessage());
		} catch (InvocationTargetException e) {
			log.error(e.getMessage());
		}
		
		//确保account 和studentAffairTeacher 的 Id为空才能正确插入
		Date date = new Date();
		account.setRegisterDate(date);
		account.setModifyDate(date);
		account.setId(null);
		dormSupervisor.setId(null);
		dormSupervisor.setAccount(account);
		dormSupervisor.setBuilding(building);
		this.systemAdministratorRepository.createDormSupervisor(dormSupervisor);
	}
	
	public void updateDormSupervisor(DormSupervisorDTO dormSupervisorInfo) throws NoSuchEntityException {
		DormSupervisor dormSupervisor = this.systemAdministratorRepository.findDormSupervisorById(dormSupervisorInfo.getId());
		
		if (dormSupervisor == null) {
			throw new NoSuchEntityException("没有该实体");
		}
		Building building = this.buildingRepository.findById(dormSupervisorInfo.getBuilding());
		
		if (building == null) {
			throw new NoSuchEntityException("没有该实体");
		}
		Account account = new Account(Role.DORM_SUPERVISOR);
		
		try {
			BeanUtils.copyProperties(account, dormSupervisorInfo);
		} catch (IllegalAccessException e) {
			log.error(e.getMessage());
		} catch (InvocationTargetException e) {
			log.error(e.getMessage());
		}
		
		//确保account 和studentAffairTeacher 的 Id为空才能正确插入
		Date date = new Date();
		account.setModifyDate(date);
		account.setId(dormSupervisorInfo.getId());
		dormSupervisor.setId(dormSupervisorInfo.getId());
		dormSupervisor.setAccount(account);
		dormSupervisor.setBuilding(building);
		this.systemAdministratorRepository.updateDormSupervisor(dormSupervisor);
	}
	
	public void deleteDormSupervisorById(Long id) {
		this.systemAdministratorRepository.deleteDormSupervisorById(id);
		
	}
	
	public void changeDormSupervisorPassword(DormSupervisor dormSupervisor,									
									  String newPassword) {
		dormSupervisor.getAccount().setPassword(newPassword);
		this.systemAdministratorRepository.updateDormSupervisor(dormSupervisor);
	}
	
	public void changeStudentAffairTeacherPassword(StudentAffairTeacher studentAffairTeacher,											
											String newPassword) {
		studentAffairTeacher.getAccount().setPassword(newPassword);
		this.systemAdministratorRepository.updateStduentAffairTeacher(studentAffairTeacher);
	}*/

}
